<?php
require_once(dirname(__FILE__) . '/include/config.inc.php');
$url = "http://www.mysd168.com/api/news/list";

function httpGet($url)
{
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_TIMEOUT, 500);
    // 为保证第三方服务器与微信服务器之间数据传输的安全性，所有微信接口采用https方式调用，必须使用下面2行代码打开ssl安全校验。
    // 如果在部署过程中代码在此处验证失败，请到 http://curl.haxx.se/ca/cacert.pem 下载新的证书判别文件。
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2);
    //curl_setopt($curl,CURLOPT_CAINFO,dirname(__FILE__).'/cacert.pem');//这是根据http://curl.haxx.se/ca/cacert.pem 下载的证书，添加这句话之后就运行正常了
    curl_setopt($curl, CURLOPT_URL, $url);

    $res = curl_exec($curl);

    curl_close($curl);

    return $res;
}

//$res=httpGet($url);


$txt_name = dirname(__FILE__) . "/txt/zs.txt";
$text = file_get_contents($txt_name);

$arr = json_decode($text, true);
$arr = $arr['data']['news'];
foreach ($arr as $key => $val) {

    $title=$val['title'];
    $content=$val['content'];
    $time=time();
    $tbid=$val['tbid'];
    $image="";
    if($val['img']!=""){
        $img=$val['img'];
        $p_arr=parse_url($img);
        $image=$p_arr['path'];
    }

    $dosql->Execute("insert into `fa_cms_archives`(channel_id,model_id,title,image,createtime,updatetime,publishtime,status,tbid) values ('7','1','$title','$image','$time','$time','$time','normal','$tbid')");
    $lastid=$dosql->GetLastID();
    $dosql->ExecNoneQuery("insert into `fa_cms_addonnews`(`id`,`content`) values ('$lastid','$content')");


    if($img!=""){
        $aa=GetRemPic($img);
    }

    echo $title."添加成功"."<br>";

}


/*
foreach ($arr as $key => $val) {



    if($val['img']!=""){
        $img=$val['img'];
        echo $img;
        $aa=GetRemPic($img);
        var_dump($aa);
    }



}*/

/*
 * 获取一个远程图片
 *
 * @access  public
 * @param   $url       string  获取字段所属模型
 * @return  $save_url  string  返回上传后地址
*/
function GetRemPic($url)
{

    $filename_arr=parse_url($url);
    $path=$filename_arr['path'];
    $filename_arr=explode('/',$path);
    $filename=array_pop($filename_arr);

    $upload_url = '/uploads/image/news/';
    $upload_dir = PHPMYWIND_UPLOAD."/images/news/";

    //上传文件路径
    $save_url = $upload_url.$filename;
    $save_dir = $upload_dir.$filename;
    echo "保存路径".$save_dir."<br/>";

    if(file_exists($save_dir)){
        return $save_url ;
    }



    //引入下载类
    require_once(PHPMYWIND_DATA.'/httpfile/down.class.php');

    //初始化变量
    $htd = new HttpDown();
    $htd->OpenUrl($url);




    //判断文件类型
    $sparr = array('image/pjpeg', 'image/jpeg', 'image/gif', 'image/png', 'image/xpng', 'image/wbmp');
    if(!in_array($htd->GetHead("content-type"), $sparr))
    {
        return FALSE;
    }
    else
    {
        $type = $htd->GetHead("content-type");

        if($type == 'image/gif')
            $tempfile_ext = 'gif';

        else if($type == 'image/png')
            $tempfile_ext = 'png';

        else if($type == 'image/wbmp')
            $tempfile_ext = 'bmp';

        else
            $tempfile_ext = 'jpg';


        echo "保存";

        $rs = $htd->SaveToBin($save_dir);
    }

    $htd->Close();
    return ($rs ? $save_url : '');
}



?>